package com.example.listviewtest;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;


public class ContactDetail extends AppCompatActivity {

    private long id=0;
    private Button cancel = null;
    private Button confirm = null;
    private EditText text_name =null;
    private EditText text_phone =null;
    private EditText text_email =null;
    private LinearLayout liner_button = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_detail);
        ImageView imageView = (ImageView) findViewById(R.id.image_head);
        text_name = findViewById(R.id.text_name);

        imageView.setImageResource(R.drawable.contact9);

        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String phone = intent.getStringExtra("phone");
        String email = intent.getStringExtra("email");
        id = (long) intent.getIntExtra("id", 0);
        text_name.setText(name);


        cancel = findViewById(R.id.edit_cancel);
        confirm = findViewById(R.id.edit_confirm);
        text_phone = (EditText) findViewById(R.id.text_phone);
        text_email = (EditText) findViewById(R.id.text_email);
        liner_button = (LinearLayout) findViewById(R.id.liner_button);

        text_phone.setText(phone);
        text_email.setText(email);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text_name.setEnabled(false);
                text_phone.setEnabled(false);
                text_email.setEnabled(false);
                liner_button.setVisibility(View.GONE);
                Toast.makeText(ContactDetail.this, "您取消了编辑", Toast.LENGTH_SHORT);
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog = new AlertDialog.Builder(ContactDetail.this)
                        .setTitle("保存修改")
                        .setMessage("确认保存新的修改？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ContentValues contact = new ContentValues();
                                //Contact contact = new Contact();
                                contact.put("name",text_name.getText().toString());
                                contact.put("phone",text_phone.getText().toString());
                                contact.put("email",text_email.getText().toString());
                                LitePal.update(Contact.class, contact, id);
                                finish();
                            }
                        })
                        .setNegativeButton("放弃", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                       .create();
                dialog.show();
            }
        });
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add_item:
                text_name = (EditText) findViewById(R.id.text_name);
                text_phone = (EditText) findViewById(R.id.text_phone);
                text_email = (EditText) findViewById(R.id.text_email);
                liner_button = (LinearLayout) findViewById(R.id.liner_button);
                text_name.setEnabled(true);
                text_phone.setEnabled(true);
                text_email.setEnabled(true);
                liner_button.setVisibility(View.VISIBLE);
                //Toast.makeText(this, "You clicked Add", Toast.LENGTH_SHORT).show();
                break;
            case R.id.remo_item:
                LitePal.deleteAll(Contact.class, "name = ?", text_name.getText().toString());
                Toast.makeText(ContactDetail.this, "删除成功", Toast.LENGTH_SHORT).show();
                //Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show();
                finish();
                break;
        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
